import { createStore,Store ,useStore as useVuexStore} from 'vuex'

import {IRootState,IStoreType} from './types'

import {loginModule} from './login/login'
import {systemModule,} from './main/system/system'
import dashboardModule from './main/analysis/dashboard'

import {getPageListData} from '@/service/main/system/system'

const store = createStore<IRootState>({
  state() {
    return {
      name: '小鹏',
      entireDepartment:[],
      entireRole:[],
      entireMenu:[]
    }
  },
  getters: {},
  actions: {
    async getInitialDataAction({commit}){
      //请求部门和角色数据
      const department = await getPageListData('/department/list',{offset:0,size:1000})
      const {list:departmentList} = department.data
      const role = await getPageListData('role/list',{offset:0,size:1000})
      const {list:roleList} = role.data

      //请求所有菜单数据
      const menuResult = await getPageListData('/menu/list',{})
      console.log(menuResult);

      const {list:menuList} = menuResult.data
      //保存数据
      commit('changeEntireDepartment',departmentList)
      commit('changeEntireRole',roleList)
      commit('changeEntireMenu',menuList)
    }
  },
  mutations: {
    changeEntireDepartment(state,list){
      state.entireDepartment = list
    },
    changeEntireRole(state,list){
      state.entireRole = list
    },
    changeEntireMenu(state,list){
      state.entireMenu = list
    }
  },
  modules:{
    loginModule,
    systemModule,
    dashboardModule
  }
})
export function setupStore(){
  store.dispatch('loginModule/loadLocalLogin')
  // store.dispatch('getInitialDataAction')
}

export function useStore():Store<IStoreType>{
  return useVuexStore()
}
export default store
